package models.resource;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.Table;

import framework.base.BaseModel;
import framework.utils.StringUtil;
import models.security.User;
import play.data.validation.Check;
import play.data.validation.CheckWith;
import play.data.validation.Match;
import play.data.validation.Max;
import play.data.validation.Required;
import play.data.validation.Unique;

@Entity
@Table(name="T_TRUNKNO")
public class TrunkNo extends BaseModel{
    //所属企业
    public String accountId;
    //中继号码
    @Required(message="号码不能为空")
    @Match(value="^([\\+][0-9]{1,3}([ \\.\\-]))?([\\(]{1}[0-9]{2,6}[\\)])?([0-9 \\.\\-/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$",message="号码格式不正确")
    @CheckWith(value=TrunkNoUniqueCheck.class,message="号码已存在")
    public String trunkNo;
    
   
    public TrunkNo(){
        this.createdAt = new Date();
        this.updatedAt = new Date();
        this.status = USE_STATUS;
    }
    
    public TrunkNo(String trunkNo){
        this();
        this.trunkNo = trunkNo;
    }
    
    
    static class TrunkNoUniqueCheck extends Check{
        @Override
        public boolean isSatisfied(Object trunkNo, Object value) {
            boolean rcode = false;
            TrunkNo  _trunk = null;
            if(trunkNo instanceof TrunkNo){
                 _trunk = (TrunkNo) trunkNo;
            }
            if(null!=_trunk){
                TrunkNo obj = TrunkNo.findByTrunkNo(value.toString());
                if(null==obj || obj.id.equals(_trunk.id)){
                    rcode = true;
                }
            }
            return rcode;
        }
    }
    
    public static TrunkNo findByTrunkNo(String trunkNo){
        String sql="from TrunkNo a where a.trunkNo=? and a.status=?";
        return TrunkNo.find(sql, trunkNo,1).first();
    }
    
    /**
     * 查询所有未分配的号码
     * @return
     */
    public static List<TrunkNo> queryAllReadyAssign(){
        String sql="from TrunkNo a where a.status=? and (a.accountId is null or a.accountId='') order by a.trunkNo";
        return TrunkNo.find(sql, USE_STATUS).fetch();
    }
    
    public static List<TrunkNo> queryByAccountId(String accountId){
        String sql="from TrunkNo a where a.status=? and a.accountId=? order by a.trunkNo";
        return TrunkNo.find(sql, USE_STATUS,accountId).fetch();
    }
    
    public static Map<String,String> queryAllReadyAssgin(){
        Map<String,String> result = new LinkedHashMap<String,String>();
        List<TrunkNo>  dataList = queryAllReadyAssign();
        for(TrunkNo no : dataList){
            result.put(no.id, no.trunkNo);
        }
        return result;
    }
    
    public static List<TrunkNo> queryAllUsed(){
       String sql="from TrunkNo a where a.status=? order by a.trunkNo";
       return TrunkNo.find(sql, USE_STATUS).fetch();
    }
    
    public static Map<String,String> queryAllNos(){
        List<TrunkNo> dataList = queryAllUsed();
        Map<String,String> result = new LinkedHashMap<String,String>();
        for(TrunkNo no : dataList){
            result.put(no.id, no.trunkNo);
        }
        return result;
       
    }
    
    
    public synchronized boolean bind(String accountId){
        boolean rcode = false;
        if(StringUtil.isBlank(this.accountId)){
            this.accountId = accountId;
            this.save();
            rcode = true;
        }
        return rcode;
    }
    
    protected void rebind(String accountId){
        this.accountId = accountId;
        this.save();
    }
    
    public void unbind(){
        this.accountId = null;
        this.save();
    }
    
    
    
    
}
